package cn.icyzx.configure;

import cn.icyzx.filter.GlobalInterceptor;
import cn.icyzx.filter.LoginInterceptor;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfig;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerView;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;

/**
 * Created by zk on 2016/11/12.
 * 这个应用上下文常用来放置 Dispatcher(一个web应用)的常用配置
 * 视图解析bean,@cn.icyzx.controller,@cn.icyzx.service,@Repository(已经被@EnableWebMvc替代扫描这三个注释的作用)
 */
@Configuration
@EnableWebMvc
@ComponentScan
@Controller
public class WebConfigure extends WebMvcConfigurerAdapter {

    private static Logger logger = Logger.getLogger(WebConfigure.class);

    @Bean
    public ViewResolver freeMarkerViewResolver() {
        FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
        resolver.setSuffix(".ftl");
        resolver.setContentType("UTF-8");
        resolver.setViewClass(FreeMarkerView.class);
        resolver.setRequestContextAttribute("request");
        return resolver;
    }


    @Bean
    public FreeMarkerConfig freeMarkerConfigurer() {
        FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
        configurer.setTemplateLoaderPath("/skin/");
        configurer.setDefaultEncoding("UTF-8");
        return configurer;
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/js/**").addResourceLocations("skin/js/");
        registry.addResourceHandler("/css/**").addResourceLocations("skin/css/");
        registry.addResourceHandler("/img/**").addResourceLocations("skin/img/");
        registry.addResourceHandler("/fonts/**").addResourceLocations("skin/fonts/");
        registry.addResourceHandler("codeMirror/**").addResourceLocations("skin/codeMirror/");
        registry.addResourceHandler("/ckeditor/**").addResourceLocations("cyzx/ckeditor/");
    }

    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new GlobalInterceptor()).addPathPatterns("/**");
        registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/setting/**");
        registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/work/publish");
        registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/work/addComment");
        registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/work/operate");
    }

}
